﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using COFE.Core;
using System.IO;
using System.Net;
using COFE.Web;

namespace COFE.Web
{
    /// <summary>
    /// Represent a COFE.IO.IFileSystemInfo entry on the web, expose it's properties, meta data and resources.
    /// </summary>
    public abstract class Entry : PropertyHost<IFileSystemInfo>, ISupportXml, ISupportLink
    {

        #region Constructor

        protected Entry()            
        {
        }

        protected Entry(IInternalFileSystemInfoExA entry)
             : base(entry)
        {           
        }

        protected Entry(MetadataList metadataList)
            : base(metadataList)
        {

        }

        #endregion

        #region Methods        
                
        public abstract Stream OpenStream(bool write = false);
        public abstract void Refresh();

        public static Entry FromCofeEntry(IFileSystemInfo entry)
        {
            return new FSIEntry(entry);
        }

        public static Entry FromXElement(string host, XElement element)
        {            
            return new WSEntry(host, element);
        }

        /// <summary>
        /// Serialize an entry.
        /// </summary>
        /// <returns></returns>
        public XElement ToXml(bool indentLinks = false)
        {
            var entryXml = new XElement(XNames.Entry,
                                 new XAttribute(XNames.ID, this.ID.ToString("n")),
                                 new XAttribute(XNames.EntryBaseType, BaseType.ToString()),
                                 new XElement(XNames.Path, Path)
                                 );
            
            writeMetadata(entryXml);            
            writeLinks(entryXml, indentLinks);

            return entryXml;
        }

        public override int GetHashCode()
        {
            return Path.GetHashCode();
        }

        public override string ToString()
        {
            return (BaseType == EntryBaseType.Folder ? "[D]" : "[F]") + Path;
        }

        public override bool Equals(object obj)
        {
            return (obj.GetType() == this.GetType())
                && (GetHashCode().Equals(obj.GetHashCode()));
        }

        #endregion

        #region Data

        //MetadataList _metadataList = null;
        List<Link> _links = null;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets id of an entry, using COFEEntry's GetHashGuid() method.
        /// </summary>
        public Guid ID { get; protected set; }
        public virtual Guid EntryTypeID { get; protected set; }
        public DateTime UtcTime { get; protected set; }
        public Int64 Length { get; protected set; }

        /// <summary>
        /// Gets File/Directory Name.
        /// </summary>
        public string Name { get; protected set; }

        /// <summary>
        /// Gets slash changed path.
        /// </summary>
        public string Path { get; protected set; }

         /// <summary>
        /// Gets whether the entry is file or folder (or unknown).
        /// </summary>
        public EntryBaseType BaseType { get; protected set; }      

        #endregion

    }
}
